package com.kingyea.mobilepolice.config.feign;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.client.Netty4ClientHttpRequestFactory;
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
import org.springframework.security.oauth2.common.AuthenticationScheme;

/**
 *
 */
@Configuration
@EnableConfigurationProperties({Oauth2ClientProperties.class})
public class Oauth2ClientConfig {

    @Autowired
    private Oauth2ClientProperties oauth2ClientProperties;
    @Value("${kingyea.mobilepolice.oauth2.server-host}")
    private String oauth2Host;
    @Value("${kingyea.mobilepolice.insideAddress}")
    private String insideAddress;

    @Bean("practiceClientCredentialsResourceDetails")
    public ClientCredentialsResourceDetails resourceDetails() {
        ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
        details.setId(oauth2ClientProperties.getId());
        details.setAccessTokenUri(oauth2ClientProperties.getAccessTokenUrl());
        details.setClientId(oauth2ClientProperties.getClientId());
        details.setClientSecret(oauth2ClientProperties.getClientSecret());
        details.setAuthenticationScheme(AuthenticationScheme.valueOf(oauth2ClientProperties.getClientAuthenticationScheme()));
        details.setTokenName("From_Client_Credentials");
        return details;
    }

    /**
     * feign 的HttpClient必须换成 Okhttp，否则可能失效,认证方必须开启client_credentials
     */
    @Bean("practiceOAuth2RestTemplate")
    @Primary
    public OAuth2RestTemplate oAuth2RestTemplate() {
        final OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resourceDetails(), new DefaultOAuth2ClientContext());
        oAuth2RestTemplate.setRequestFactory(new Netty4ClientHttpRequestFactory());
        return oAuth2RestTemplate;

    }

    /**
     * feign 的HttpClient必须换成 Okhttp，否则可能失效,认证方必须开启client_credentials
     */
    @Bean("practiceOAuth2RestTemplateNei")
    public OAuth2RestTemplate oAuth2RestTemplateNei() {
        final OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resourceDetailsNei(), new DefaultOAuth2ClientContext());
        oAuth2RestTemplate.setRequestFactory(new Netty4ClientHttpRequestFactory());
        return oAuth2RestTemplate;

    }

    /**
     * @Description: 内外网需要有同样的客户端认证方式
     * @Param: []
     * @return: org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails
     * @Author: cxc
     * @Date: 2018.04.24 18:30
     */
    public ClientCredentialsResourceDetails resourceDetailsNei() {
        ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
        details.setId(oauth2ClientProperties.getId());
        details.setAccessTokenUri(insideAddress + oauth2ClientProperties.getAccessTokenUrl().substring(oauth2ClientProperties.getAccessTokenUrl().indexOf("/", 8)));//神奇数字
//        details.setAccessTokenUri(oauth2ClientProperties.getAccessTokenUrl().replace ( oauth2Host, insideAddress+"/uaa" ));// 迷之字符串  // "http://localhost:8784/uaa/oauth/token"
        details.setClientId(oauth2ClientProperties.getClientId());
        details.setClientSecret(oauth2ClientProperties.getClientSecret());
        details.setAuthenticationScheme(AuthenticationScheme.valueOf(oauth2ClientProperties.getClientAuthenticationScheme()));
        details.setTokenName("From_Client_Credentials");
        return details;
    }


}